import { _decorator, CacheMode, Camera, Component, EventTouch, Node, RigidBody2D, UITransform, v2, v3, Vec2 } from 'cc';
import { GameMgr } from '../script/GameMgr';
import { GameMgrShi } from './GameMgrShi';
const { ccclass, property } = _decorator;

@ccclass('TouchMoveShi')
export class TouchMoveShi extends Component {

    @property(Node)
    player: Node;
    @property(Camera)
    cameraNode: Camera;

    tempPos = v3();
    touchStartPos = v3(0, 0, 0);
    direction;
    moveSpeed = 5

    static ins: TouchMoveShi = null;
    protected onLoad(): void {
        TouchMoveShi.ins = this;
    }
    protected start(): void {
        this.node.on(Node.EventType.TOUCH_START, this.touchStartFunc, this)
        this.node.on(Node.EventType.TOUCH_MOVE, this.touchMoveFunc, this)
        this.cameraNode.getComponent(Camera).orthoHeight = 667

    }

    touchStartFunc(event: EventTouch) {
        console.log("触摸点击")
        let touchPos = event.getUILocation();
        let canvasTouchPos = this.node.getComponent(UITransform).convertToNodeSpaceAR(v3(touchPos.x, touchPos.y, 0))
        this.touchStartPos = canvasTouchPos;


    }

    touchMoveFunc(event: EventTouch) {

        let movePos = this.node.getComponent(UITransform).convertToNodeSpaceAR(v3(event.getUILocation().x, event.getUILocation().y, 0))
        let ox = this.touchStartPos.x - movePos.x;
        let oy = this.touchStartPos.y - movePos.y;

        this.direction = v2(ox, oy).normalize()


    }

    protected update(dt: number): void {
        if (GameMgrShi.ins.isGameIng) {
            if (this.direction) {
                this.player.setPosition(this.player.getPosition().x - this.direction.x * this.moveSpeed, this.player.getPosition().y - this.direction.y * this.moveSpeed)
                if (Math.abs(this.player.position.x)>GameMgrShi.ins.mapBoundary.x) {
                    if (this.player.position.x>0) {
                    this.player.setPosition(GameMgrShi.ins.mapBoundary.x,this.player.position.y)

                    }
                    else{
                    this.player.setPosition(-GameMgrShi.ins.mapBoundary.x,this.player.position.y)
                    }
                }

                if (Math.abs(this.player.position.y)>GameMgrShi.ins.mapBoundary.y) {
                    if (this.player.position.y>0) {
                    this.player.setPosition(this.player.position.x,GameMgrShi.ins.mapBoundary.y)

                    }
                    else{
                    this.player.setPosition(this.player.position.x,-GameMgrShi.ins.mapBoundary.y)
                    }
                }

                this.cameraNode.node.setPosition(this.player.getPosition())

                this.lookAtTarget(v2(this.player.getPosition().x - this.direction.x * this.moveSpeed, this.player.getPosition().y - this.direction.y * this.moveSpeed), this.player, 20);
                // if (GameMgrShi.ins.addtionBiontRoot.children.length > 0) {
                //     for (let i = 0; i < GameMgrShi.ins.addtionBiontRoot.children.length; i++) {
                //         let chi = GameMgrShi.ins.addtionBiontRoot.children[i]
                //         this.lookAtTarget(v2(this.player.getPosition().x - this.direction.x * this.moveSpeed, this.player.getPosition().y - this.direction.y * this.moveSpeed), chi, 20);

                //     }
                // }

            }

        }

    }


    trackTarget(targetX: number, targetY: number, ownX: number, ownY: number, speed: number = 1) {

        let deltax: number = targetX - ownX;

        let deltay: number = targetY - ownY;

        if (deltax == 0) {

            if (targetY >= ownY) {

                deltax = 0.0000001;
            } else {
                deltax = -0.0000001;
            }
        }

        if (deltay == 0) {

            if (targetX >= ownX) {

                deltay = 0.0000001;
            } else {

                deltay = -0.0000001;
            }
        }

        let angle: number = 0;
        // cc_log(deltax + "===" + deltay)
        if (deltax > 0 && deltay > 0) {

            angle = Math.atan(Math.abs(deltay / deltax));       //第一项限
        } else if (deltax < 0 && deltay > 0) {

            angle = Math.PI - Math.atan(Math.abs(deltay / deltax));   //第二项限
        } else if (deltax < 0 && deltay < 0) {

            angle = Math.PI + Math.atan(Math.abs(deltay / deltax))    //第三项限
        } else {

            angle = 2 * Math.PI - Math.atan(Math.abs(deltay / deltax))         // 第四项限
        }

        return [speed * (Math.cos(angle)), speed * (Math.sin(angle)), angle];
    }

    //面向敌人
    lookAtTarget(targetPos, selfNode, speed) {

        if (!targetPos) {
            return
        }

        let speedArr = this.trackTarget(targetPos.x, targetPos.y, selfNode.position.x, selfNode.position.y, speed);

        selfNode.angle = speedArr[2] * (180 / Math.PI);

    }

}


